package com.ruoyi.web.controller.monggo;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.mongodb.Users;
import com.ruoyi.system.mongo.StudentDao;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import com.ruoyi.system.service.impl.SendSmsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author HP
 * @Date 2021/3/26
 * @Description TODO
 */
@RestController
@RequestMapping("/system")
public class UsersController {
    @Autowired
    private StudentDao studentDao;
    @Autowired
    RedisTemplate<String,String> redisTemplate;


    @ApiOperation("判断用户是否认证")
    @GetMapping("/queryUsersPhoneNum")
    public AjaxResult queryUsersPhoneNum(String userId){
        Users users = studentDao.selectUsersByUserId(userId);
        if (StringUtils.isBlank(users.getPhoneNum())){
            return AjaxResult.error("未认证");
        }else{
            return AjaxResult.success("已认证");
        }
    }



    @RequestMapping("/queryUsers")
    public AjaxResult queryUsers(String username,String phoneNum,String code){
        //判断验证码
        if(!code.equals(redisTemplate.opsForValue().get(phoneNum))){
            return AjaxResult.error(300,"验证码错误");
        }
        Users users = studentDao.queryUsers(username, phoneNum);
        if (users==null){
            return AjaxResult.error();
        }else{
            return AjaxResult.success();
        }
    }
    @PutMapping("/updatePassword")
    public AjaxResult updatePassword(String username,String phoneNum, String email,String password) throws Exception {
        studentDao.updatePassword(username, phoneNum, email, password);
        return AjaxResult.success();
    }
}
